package foundation.back_access;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 回形取数就是沿矩阵的边取数，若当前方向上无数可取或已经取过，则左转90度。一开始位于矩阵左上角，方向向下。
 */
public class Main {
    static int count; // 记录已经遍历过的数字个数
    static int m;
    static int n;
    // 控制上下左右可以遍历的范围，都是可以取到的
    static int left = 0;
    static int right;
    static int up = 0;
    static int down;
//    static StringBuilder res = new StringBuilder();
    static List<Integer> put = new ArrayList<>();
    static int[][] nums;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        down = m - 1;
        n = sc.nextInt();
        right = n - 1;
        nums = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                nums[i][j] = sc.nextInt();
            }
        }

//        res.append(nums[0][0]);
//        System.out.print(nums[0][0]);
        put.add(nums[0][0]);
        count = 1;
        getNum(0, 0, 1);
//        System.out.println(res.toString());
        for (int i = 0; i < m * n; i++) {
            System.out.print(put.get(i));
            if (i != m * n - 1) {
                System.out.print(" ");
            }
        }
    }

    /**
     * 递归返回回形路线
     * @param row
     * @param col
     * @param direction 控制当前方向 1代表下 2代表右 3代表上 4代表左
     * @return
     */
    public static void getNum(int row, int col, int direction) {
        if (count == m * n) {
            return;
        } else {
            // 下-》右-》上-》左
            if (direction == 1) {
                if (isLegal(row + 1, col)) {
                    row += 1;
//                    res.append(" " + nums[row][col]);
//                    System.out.print(" " + nums[row][col]);
                    put.add(nums[row][col]);
                    count ++;
                    getNum(row, col, direction); // 保持方向一致
                } else {
                    // 此时这次下走完了，将left+1
                    left ++;
                    // 不能再继续下的方向，变为左的方向
                    getNum(row, col, 2);
                }
            } else if (direction == 2) {
                if (isLegal(row, col + 1)) {
                    col += 1;
//                    res.append(" " + nums[row][col]);
//                    System.out.print(" " + nums[row][col]);
                    put.add(nums[row][col]);
                    count ++;
                    getNum(row, col, direction);
                } else {
                    down --;
                    getNum(row, col, 3);
                }
            } else if (direction == 3) {
                if (isLegal(row - 1, col)) {
                    row -= 1;
//                    res.append(" " + nums[row][col]);
//                    System.out.print(" " + nums[row][col]);
                    put.add(nums[row][col]);
                    count ++;
                    getNum(row, col, direction);
                } else {
                    right --;
                    getNum(row, col, 4);
                }
            } else {
                if (isLegal(row, col - 1)) {
                    col -= 1;
//                    res.append(" " + nums[row][col]);
//                    System.out.print(" " + nums[row][col]);
                    put.add(nums[row][col]);
                    count ++;
                    getNum(row, col, direction);
                } else {
                    up ++;
                    getNum(row, col, 1);
                }
            }
        }
    }

    public static boolean isLegal(int row, int col) {
        if (row < up || row > down || col < left || col > right) {
            return false;
        }
        return true;
    }
}
